/******************************************************************************/
/* Frank Fossen, Levent Neyse, and Carsten Schroeder                          */
/* Does Cognitive Reflection Relate to Preferences & Socio-Economic Outcomes? */
/* Journal of Political Economy Microeconomics (accepted 2024)                */
/******************************************************************************/

/******************************************************************************/
/* Analysis                                                                   */
/******************************************************************************/

set varabbrev off
set more off

clear

cap log close
log using "${MY_RES_PATH}02_estim_CRT.log", text replace


/* Variable list */

#delimit ;

local depvars "riskpref patience_fwd impulsive_fwd altruism_fwd trust_fwd 
	reciprocity grossinc finassets_fwd polRight_fwd religion health lifeSatis
	civilServ saving donations_fwd friends";

global controlvars "female age agesq highschool apprenticeship highertechncol
                    university";

/* For robustness check "controlling for response time" in Tab. A4, add to 
   global controlvars: timeCRT */
		
#delimit cr


/* Load data */

use "${MY_DATA_PATH}SOEPIS2020prep.dta" if syear==2020


/* Drop those who say they knew the CRT questions */

drop if questionsKnown==1


/* For robustness checks in Tab. A4, drop quick respondents or 
   unmotivated respondents: */
* drop if fastCRT==1
* drop if unmotivatedCRT==1


tab female, mis

/* For heterogeneity analysis by gender in Tab. A5, drop one gender: */
* drop if female==1
* drop if female==0


/********************ANALYSIS***********************/


/****************/
/* Descriptives */
/****************/

#delimit ;

/* Determine analysis sample */
reg correctCount ${controlvars};
gen esample = e(sample);

sum age if esample==1;

di "Descriptive statistics (Tab. 2):";
tabstat correctCount ballCorrect machineCorrect liliesCorrect `depvars' ${controlvars}
	if esample==1
	, by(female) stats(mean sd N) columns(statistics) longstub;

di "Descriptive statistics for college students (Tab. A3):";
tabstat correctCount ballCorrect machineCorrect liliesCorrect `depvars' ${controlvars}
	if esample==1 & student2020==1
	, by(female) stats(mean sd N) columns(statistics) longstub;
	
	
di "Correlation matrix (Tab. 3):";
pwcorr correctCount ballCorrect machineCorrect liliesCorrect `depvars' ${controlvars}
	if esample==1
	, sig;


histogram correctCount if esample==1, 
	xtitle(CRT score)
	scheme (s2mono) saving("${MY_RES_PATH}histogramCRT", replace);
graph export "${MY_RES_PATH}histogramCRT.pdf", replace;

tab correctCount if esample==1;


#delimit cr


/**************
* Estimations *
***************/

* Critical values for Minimum Detectable Effect Size:
scalar MDEcv005 = 3.65
scalar MDEcv05 = 2.8

gen ivsample=.

corr `depvar' wordsCorr_fwd numbersCorr_fwd

foreach depvar in `depvars' {
	reg `depvar' correctCount ${controlvars}, vce(robust)
	estadd ysumm, mean
	sum `depvar' if e(sample)==1
	estadd scalar sdDepVar = r(sd)
	estadd scalar MDEcv005 = _se[correctCount]*MDEcv005/e(sdDepVar)
	estadd scalar MDEcv05 = _se[correctCount]*MDEcv05/e(sdDepVar)
	est store ec_`depvar'
	
	ivregress gmm `depvar' (correctCount = wordsCorr_fwd numbersCorr_fwd) ${controlvars}, first vce(robust)
	estadd ysumm, mean
	sum `depvar' if e(sample)==1
	estadd scalar sdDepVar = r(sd)
	estadd scalar MDEcv005 = _se[correctCount]*MDEcv005/e(sdDepVar)
	estadd scalar MDEcv05 = _se[correctCount]*MDEcv05/e(sdDepVar)
	estat firststage
	estadd scalar firstStageF = r(singleresults)[1,4]
	estat overid
	estadd scalar HansenJ = r(HansenJ)	
	estadd scalar p_HansenJ = r(p_HansenJ)	
	est store iv_`depvar'
	replace ivsample=e(sample)
}

#delimit ;

esttab ec_riskpref ec_patience_fwd ec_impulsive_fwd ec_altruism_fwd ec_trust_fwd
	ec_reciprocity ec_grossinc ec_finassets_fwd ec_polRight_fwd ec_religion ec_health 
	ec_lifeSatis ec_civilServ ec_saving ec_donations_fwd ec_friends
	using "${MY_RES_PATH}mainResults.rtf", 
	replace
	b se
	star(* 0.05 ** 0.005)
	title("Effects of CRT scores on various outcomes (with control variables)")
	mtitles
	scalars(N ymean r2 MDEcv05 MDEcv005)
	compress nonumbers nogaps noomitted nobaselevels
	nonotes
;

esttab iv_riskpref iv_patience_fwd iv_impulsive_fwd iv_altruism_fwd iv_trust_fwd
	iv_reciprocity iv_grossinc iv_finassets_fwd iv_polRight_fwd iv_religion iv_health 
	iv_lifeSatis iv_civilServ iv_saving iv_donations_fwd iv_friends
	using "${MY_RES_PATH}iv.rtf", 
	replace
	b se
	star(* 0.05 ** 0.005)
	title("Effects of CRT scores on various outcomes (using cognitive ability as IV for CRT)")
	mtitles
	scalars(N ymean r2 MDEcv05 MDEcv005 firstStageF p_HansenJ)
	compress nonumbers nogaps noomitted nobaselevels
	nonotes
;


/* Joint test of all null hypotheses: */
mvreg `depvars' = correctCount ${controlvars};

test _b[riskpref:correctCount]=
	_b[patience_fwd:correctCount]=
	_b[impulsive_fwd:correctCount]=
	_b[altruism_fwd:correctCount]=
	_b[trust_fwd:correctCount]=
	_b[reciprocity:correctCount]=
	_b[grossinc:correctCount]=
	_b[finassets_fwd:correctCount]=
	_b[polRight_fwd:correctCount]=
	_b[religion:correctCount]=
	_b[health:correctCount]=
	_b[lifeSatis:correctCount]=
	_b[civilServ:correctCount]=
	_b[saving:correctCount]=
	_b[donations_fwd:correctCount]=
	_b[friends:correctCount]=
	0;


/* Multiple hypothesis testing: */;
	
/* For OLS: */

mhtreg (riskpref correctCount ${controlvars}) 
	(patience_fwd correctCount ${controlvars})
	(impulsive_fwd correctCount ${controlvars})
	(altruism_fwd correctCount ${controlvars})
	(trust_fwd correctCount ${controlvars})
	(reciprocity correctCount ${controlvars})
	(grossinc correctCount ${controlvars})
	(finassets_fwd correctCount ${controlvars})
	(polRight_fwd correctCount ${controlvars})
	(religion correctCount ${controlvars})
	(health correctCount ${controlvars})
	(lifeSatis correctCount ${controlvars})
	(civilServ correctCount ${controlvars})
	(saving correctCount ${controlvars})
	(donations_fwd correctCount ${controlvars})
	(friends correctCount ${controlvars}), 
	robust
	seed(20220801)
	bootstrap(6000);

/* For IV: */
rwolf `depvars', 
	method(ivregress)
	iv(wordsCorr_fwd numbersCorr_fwd)
	indepvar(correctCount) 
	controls(${controlvars})
	verbose
	seed(20220801) 
	reps(12000)
	vce(robust)
	;
;

#delimit cr

log close
